/* ----------------------------------------------------------------------- *
 *                                                                         *
 *    (C) Copyright 1996 by:  aiNet, Ales Krajnc, s.p.,                    *
 *										Trubarjeva 42, SI-3000 Celje,                *
 *										Europe, Slovenia                             *
 *     All Rights Reserved                                                 *
 *                                                                         *
 *     Subject: C code for single vector prediction.                       *
 *     File:    DLLTST02 - The XOR problem with dynamic model creation     *
 *                                                                         *
 * ----------------------------------------------------------------------- */

#include "ainetdll.h"
#include <stdio.h>
#include <stdlib.h>

/*--------------------------------------------------------------------------
	Here it will be shown how we can solve the XOR problem using
	aiNet C functions.

	The XOR problem:
	================
		Number of model vectors: 4
			 Number of variables: 3
	 Number of input variables: 3
		 Any discrete variables: NONE

		Model vectors:  Inp,Inp,Out
				  row 1:  1,  1,  0
				  row 2:  1,  0,  1
				  row 3:  0,  1,  1
				  row 4:  0,  0,  0

	Test vectors (vectors which will be used in prediction) together with
	penalty coefficient and penalty method.

		 Prediction vectors:  Inp  Inp  Out
						  prd 1:  0.9  0.1  ??
						  prd 2:  0.1  0.9  ??
						  prd 3:  0.2  0.2  ??
						  prd 4:  0.7  0.7  ??

		 Penalty coeffcient: 1.5
		 Penalty methods:    NEAREST

	NOTE: Selected penalty coefficients are in no case optimal.
			They were selected randomly, to perform a few tests.
			The test results were compared with the results calculated by
			the main aiNet 1.14 application.

	--------------------------------------------------------------------------
	Results (rounded at fourth decimal):
	--------------------------------------------------------------------------

		 Penalty cefficient: 1.5
		 Penalty method:     NEAREST N.
												  (RESULT)
		 Prediction vectors:  Inp  Inp  (  Out )
						  prd 1:  0.9  0.1  (0.9989)
						  prd 2:  0.1  0.9  (0.9989)
						  prd 3:  0.2  0.2  (0.1054)
						  prd 4:  0.7  0.7  (0.3449)

	-------------------------------------------------------------------------*/

void main()
{
	int i;
	int version;

	/*
	 * vectors to be predicted
	 */

	float predict[4][3] = { { 0.9,0.1, 999 },
									{ 0.1,0.9, 999 },
									{ 0.2,0.2, 999 },
									{ 0.7,0.7, 999 } };
	/*
	 * Model variable
	 */

	aiModel* model;

	/*
	 * Title
	 */

	version = aiGetVersion();
	printf( "\naiNetDLL version %i.%i! (C) Copyright by aiNet, 1996",
			  version/100, version%100 );
	printf( "\n---------------------------------------------------\n" );

	/*
	 * Register DLL
	 */

	aiRegistration( "Your registration name", "Your code" );

	/*
	 * Allocate the model variable and necessary memory
	 */

	model = aiCreateModel(4,   /* 4 model vectors   */
								 3,   /* 3 variables       */
								 2);  /* 2 input variables */
	if(!model) {
		printf( "\nError: Something went wrong during model creation!" );
		exit(EXIT_FAILURE);
	}

	/*
	 * Loading data into the model using aiSetVariable function
	 */

	aiSetVariable(model,1,1, 1.0); /* first model vector */
	aiSetVariable(model,1,2, 1.0); /* 1 xor 1 = 0        */
	aiSetVariable(model,1,3, 0.0);

	aiSetVariable(model,2,1, 1.0); /* second model vector */
	aiSetVariable(model,2,2, 0.0); /* 1 xor 0 = 1         */
	aiSetVariable(model,2,3, 1.0);

	aiSetVariable(model,3,1, 0.0); /* third model vector */
	aiSetVariable(model,3,2, 1.0); /* 0 xor 1 = 1        */
	aiSetVariable(model,3,3, 1.0);

	aiSetVariable(model,4,1, 0.0); /* fourth model vector */
	aiSetVariable(model,4,2, 0.0); /* 0 xor 0 = 0         */
	aiSetVariable(model,4,3, 0.0);

	/*
	 * Output the model
	 */

	printf( "\n             Model name: aiNet DLL test 2 (aiCreateModel)" );
	printf( "\nNumber of model vectors: %i", aiGetNumberOfModelVectors(model));
	printf( "\n    Number of variables: %i", aiGetNumberOfVariables(model));
	printf( "\n         Variable names: A,   B,   A xor B" );
	printf( "\n          Discrete flag: %i,   %i,   %i",
			  aiGetDiscreteFlag(model,1),
			  aiGetDiscreteFlag(model,2),
			  aiGetDiscreteFlag(model,3) );
	for( i=1; i<=aiGetNumberOfModelVectors(model); i++ ) {
		printf( "\n\t\t\t %3.1lf, %3.1lf, %3.1lf",
				  aiGetVariable(model, i,1),
				  aiGetVariable(model, i,2),
				  aiGetVariable(model, i,3) );
	}

	/*
	 * Normalize the model
	 */

	aiNormalize(model, NORMALIZE_REGULAR);

	/*
	 * Prediction
	 * This test has nearest neighbour penalty coefficient 1.50
	 */

	printf( "\n\n  Penalty coefficient: 1.50" );
	printf(   "\n       Penalty method: NEAREST N." );
	printf(   "\n\t A(inp), B(inp), A xor B(out)" );
	for ( i=0; i<4; i++ ) {
		aiPrediction(model, predict[i], 1.50, PENALTY_NEAREST);
		printf( "\n\t%7.4lf, %7.4lf, %7.4lf",
				  predict[i][0],predict[i][1],predict[i][2] );
	}

	/*
	 * Denormalize the model (in this case it is not necessary)
	 */

	aiDenormalize(model);

	/*
	 * Free alocated memory
	 */

	aiDeleteModel(model);

	printf( "\n\nEnd." );
	exit(EXIT_SUCCESS);
}

/* THE END */
